package com.sys.midware.hbase.util;

/**
 * 用于生成文档唯一标示key的字符串的类
 * 
 * @author guangfeng
 * 
 */
public class DocKeyUtil {

	private static final String delimiter = ":";

	/**
	 * 生成rowKey字符串
	 * 
	 * @param categoryKey
	 *            类目key
	 * @param listPageKey
	 *            列表页key
	 * @param listPageIdx
	 *            列表页index
	 * @param detailPageKey
	 *            详情页key
	 * @return
	 */
	public static String getRowKey(int categoryKey, int listPageKey,
			int listPageIdx, int detailPageKey) {
		StringBuilder sb = new StringBuilder();
		sb.append(String.valueOf(categoryKey)).append(delimiter);
		sb.append(String.valueOf(listPageKey)).append(delimiter);
		sb.append(String.valueOf(listPageIdx)).append(delimiter);
		sb.append(String.valueOf(detailPageKey));

		return sb.toString();
	}

	/**
	 * 生成rowKey字符串
	 * 
	 * @param categoryKey
	 *            类目key
	 * @param listPageKey
	 *            列表页key
	 * @param listPageIdx
	 *            列表页index
	 * @param detailPageKey
	 *            详情页key
	 * @return
	 */
	public static String getRowKey(String categoryKey, String listPageKey,
			String listPageIdx, String detailPageKey) {
		StringBuilder sb = new StringBuilder();
		sb.append(String.valueOf(categoryKey)).append(delimiter);
		sb.append(String.valueOf(listPageKey)).append(delimiter);
		sb.append(String.valueOf(listPageIdx)).append(delimiter);
		sb.append(String.valueOf(detailPageKey));

		return sb.toString();
	}

	/**
	 * 根据原有的 rowkey 生成新的记录
	 * 
	 * @param catetoryKey
	 *            ,原始 catetoryKey
	 * @param url
	 *            添加的 url
	 * @return String
	 */
	public static String getRowKey(String catetoryKey, String url, int index) {

		StringBuilder sb = new StringBuilder();
		sb.append(String.valueOf(catetoryKey)).append(delimiter);
		sb.append(String.valueOf(url.hashCode())).append(delimiter);
		sb.append(String.valueOf(index)).append(delimiter);
		sb.append(String.valueOf(0));
		return sb.toString();
	}

	/**
	 * 根据原有的 rowkey 生成新的记录
	 * 
	 * @param rowkey
	 *            ,原始 rowkey
	 * @param index
	 *            添加的 类表页的 序列号
	 * @return String
	 */
	public static String getRowKey(String rowKey, int index) {

		String[] rowkeys = rowKey.split(delimiter);
		if (rowkeys.length != 4) {
			throw new IllegalArgumentException(
					"rowkey is incorrect formation [XX:XX:XX:XX]:" + rowKey);
		}

		StringBuilder sb = new StringBuilder();
		sb.append(rowkeys[0]).append(delimiter);
		sb.append(rowkeys[1]).append(delimiter);
		sb.append(String.valueOf(index)).append(delimiter);
		sb.append(String.valueOf(0));
		return sb.toString();
	}

	/**
	 * 获取 rowkey catetory 头信息
	 * 
	 * @param rowKey
	 *            ,hbase 记录 rowKey
	 * @return categorykey , 参数有误返回 null
	 */
	public static String getRowKeyCatetory(String rowKey) {
		if (null == rowKey || rowKey.trim().isEmpty()) {
			return null;
		}
		String splits[] = rowKey.split(":");
		if (splits.length != 4) {
			throw new IllegalArgumentException(
					"rowkey is incorrect formation [XX:XX:XX:XX]:" + rowKey);
		}
		return splits[0];
	}

	/**
	 * 获取 rowkey link hash 头信息
	 * 
	 * @param rowKey
	 *            ,hbase 记录 rowKey
	 * @return list link hash , 参数有误返回 null
	 */
	public static String getRowKeyLinkHash(String rowKey) {
		if (null == rowKey || rowKey.trim().isEmpty()) {
			return null;
		}
		String splits[] = rowKey.split(":");
		if (splits.length != 4) {
			throw new IllegalArgumentException(
					"rowkey is incorrect formation [XX:XX:XX:XX]:" + rowKey);
		}
		return splits[1];
	}

	/**
	 * 获取 rowkey link index 头信息
	 * 
	 * @param rowKey
	 *            ,hbase 记录 rowKey
	 * @return list link hash , 参数有误返回 null
	 */
	public static String getRowKeyLinkIndex(String rowKey) {
		if (null == rowKey || rowKey.trim().isEmpty()) {
			return null;
		}
		String splits[] = rowKey.split(":");
		if (splits.length != 4) {
			throw new IllegalArgumentException(
					"rowkey is incorrect formation [XX:XX:XX:XX]:" + rowKey);
		}
		return splits[2];
	}

	/**
	 * 获取 rowkey link detail part 头信息
	 * 
	 * @param rowKey
	 *            ,hbase 记录 rowKey
	 * @return rowkey link detail part , 参数有误返回 null
	 */
	public static String getRowKeyDetail(String rowKey) {
		if (null == rowKey || rowKey.trim().isEmpty()) {
			return null;
		}
		String splits[] = rowKey.split(":");
		if (splits.length != 4) {
			throw new IllegalArgumentException(
					"rowkey is incorrect formation [XX:XX:XX:XX]:" + rowKey);
		}
		return splits[3];
	}

}
